#pragma once 
#include "../server.hpp"

class EchoServer
{
private:
    TcpServer m_server;
private:
    void OnMessage(const PtrConnection& conn, Buffer* buf)
    {
        conn->Send(buf->ReaderPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        //conn->Shutdown();
    } 
    void OnClosed(const PtrConnection& conn)
    {
        DBG_LOG("Close Connecion: %p", conn.get());
    } 
    void OnConnected(const PtrConnection& conn)
    {
        DBG_LOG("New Connection: %p", conn.get());
    }
public:
    EchoServer(int port)
        :m_server(port)
    {
        m_server.SetThreadCount(2);
        m_server.EnableInactiveRelease(10);
        m_server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        m_server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        m_server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    ~EchoServer() {}

    void Start() { return m_server.Start(); }
};